package servlets;

import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.BadgeManager;
import managers.JsonManager;
import structures.GamifyResult;

public class GetUserBadgesNameAndUrlServlet extends HttpServlet {
	private String schemaName;
	private static final long serialVersionUID = 3453091955573157294L;	
 	
	/**
	 * This method initialize the schema name
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
        ServletContext servletContext;
        servletContext = getServletContext(); 
        schemaName = servletContext.getInitParameter("SchemaName");
     }
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		
		System.setProperty("jsse.enableSNIExtension", "false");

		String idUser, jsonResult;
		ArrayList<GamifyResult> gamifyResults = new ArrayList<GamifyResult>(); 
		BadgeManager bm = new BadgeManager();
		JsonManager jsonManager;
		jsonManager = new JsonManager();
		idUser=request.getParameter("idUser");
		try {
			gamifyResults = bm.getUserBadges(idUser, schemaName);
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		jsonResult = jsonManager.generateJSON(gamifyResults);
		jsonResult = "{\n\t\"result\": " + jsonResult + " \n}";
		
		System.out.println(jsonResult);
		System.out.flush();
		
		
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");		
		try{
					response.getWriter().write(jsonResult);
			}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}


